package com.prince.util;

import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created with IntelliJ IDEA.
 * User: william
 * Date: 13-5-9
 * Time: 下午3:14
 * Mobi:18601920351 Email: lishu@qdcf.com
 */
public class MD5Util {
    //DESede 加密解密key
    private static String key= "Y2M3ZWZHNZC5ZGU3Y2JKYZMXMGQYN2RHOWU5ZDIWOTQ=";
    private static String Algorithm = "desede/CBC/PKCS5Padding"; // 定义 加密算法,可用DES,DESede,Blowfish
    private static String encoding = "utf-8";
    private static String iv = "01234567";
    /**
     * MD5运算
     *
     * @param s
     * @return
     */
    public static byte[] MD5Bytes(String s) {
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("MD5");
            digest.update(s.getBytes());
            return digest.digest();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }
    
	 /**
     * 先Md5再Base64加密
     * 用于密码加密
     */
    public static String encryptByMd5AndBASE64(String pwd) {
        try {
            byte messageDigest[] = MD5Util.MD5Bytes(pwd);
            return Base64.encode(messageDigest);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 功能描述: DESede数据加密
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/3/1 15:42
     */
    public static String encrypt(String info) {
        if(StringUtil.isEmpty(info)){
            return "";
        }
        Key deskey = null;
        try {
            DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory
                    .getInstance("desede");
            deskey = keyfactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance(Algorithm);
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
            byte[] encryptData = cipher.doFinal(info.getBytes(encoding));
            return new String(Hex.encodeHex(encryptData));//Base64.encode(encryptData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 功能描述: DESede数据解密
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/3/1 15:42
     */
    public static String decode(String encrypt) {
        if(StringUtil.isEmpty(encrypt)){
            return "";
        }
        Key deskey = null;
        try {
            DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory
                    .getInstance("desede");
            deskey = keyfactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance(Algorithm);
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
            byte[] decryptData = cipher.doFinal(Hex.decodeHex(encrypt.toCharArray()));//Base64.decode(encryptText)
            return new String(decryptData, encoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    
    public static void main(String[] args) {
    	//加密
		String a = "123123";
		System.err.println(Encrypt.e(a));  //4297f44b13955235245b2497399d7a93
		byte[] md5Bytes = MD5Util.MD5Bytes(a);
		System.out.println(md5Bytes);  //[B@15db9742
		String encode = Base64.encode(md5Bytes);
		System.out.println(encode);   //Qpf0SxOVUjUkWySXOZ16kw==
	
		//解密
		byte[] decode = Base64.decode(encode);
		System.out.println(decode);   //[B@6d06d69c

		System.err.println(MD5Util.encryptByMd5AndBASE64("123123"));

		String dd = MD5Util.encrypt("123121231231233");
        System.out.println(dd);
        System.out.println(MD5Util.decode(dd));
	}
    
}
